/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Portions created by the Initial Developer are
 * Copyright (C) 2012 the Initial Developer.
 * All Rights Reserved.
 *
 * The Original Code is this.
 *
 * The Initial Developer of the Original Code is the author below.
 */ 
 /**
 * @file .h
 * @brief auto generated.
 * @author K.Ozaki
 * @date 2011/03/11
 */


/*
 * @file MessageEvent.h
 *
 * @date: 2011/03/11
 * @author: ozaki
 */

#ifndef MESSAGEEVENT_H_
#define MESSAGEEVENT_H_

#include "semi/posix/MessageTimer.h"
#include "semi/logg/Logger.h"
#include "sys/wait.h"
// #include "sys/types.h"

namespace semi {

class MessageParam {
public:
    //    MessageParam(std::string taskID);
    MessageParam();
    ~MessageParam();
    //    std::string taskid;
    std::string message;
    posix::MessageTimer* timer;
    //    int sleep;
    //    posix::SignalAction* action;
};

class MessageEvent {
public:

    MessageEvent();
    MessageEvent(std::string task_id);
    ~MessageEvent();

    void init(std::string task_id);
    void sendMessage(std::string message);
    void reserveMessage(int sleepsec, std::string message);
    //    void reserveMessage(util::DateUtil date, std::string message);
    bool isReserved();

    std::string waitMessage();
    //    bool alreadyListen();

    void remove();
    void terminate();

    static void terminateAll(conf::Configurator::TaskConfigs tconfs);
    static void terminateAll();

private:

    std::string taskId;

    // pthread_t tid;

    //    posix::Semaphore sem; // for wait
    posix::MessageQue* quep;
    posix::MessageTimer* tmrp;
    logg::Logger log;
    MessageParam* param;

    bool sweepChild();
    //    int getWaitCount();
    static void * threadingProcess(void* param);

    semi::util::Ints pidList;
};

}

#endif /* MESSAGEEVENT_H_ */
